<template>
  <div class="employees">
    <el-card>
      <PageTools>
        <template #right>
          <div>
            <!-- <el-button
              v-showBtn="'aa'"  自定义指令实现按钮权限管理
              type="primary"
              @click="
                $router.push(
                  '/employees/upload?name=' + encodeURIComponent('员工')
                )
              "
            >导入</el-button> -->
            <!-- 指令实现按钮权限 -->
            <el-button
              v-if="showBtn('aa')"
              type="primary"
              @click="
                $router.push(
                  '/employees/upload?name=' + encodeURIComponent('员工')
                )
              "
            >导入</el-button>
            <el-button type="primary" @click="addClick">+ 新增员工</el-button>
          </div>
        </template>
      </PageTools>
    </el-card>
    <el-card style="margin-top: 15px">
      <el-table :data="list">
        <el-table-column label="序号" width="50">
          <template v-slot="{ $index }">
            <div>
              {{ (page.page - 1) * page.size + $index + 1 }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="姓名" prop="username" />
        <el-table-column label="手机号" prop="mobile" />
        <el-table-column label="员工头像">
          <template v-slot="{ row }">
            <img
              v-imgErr="defaultImg"
              :src="row.staffPhoto"
              alt=""
              class="img"
              @click="showQrcode(row.staffPhoto)"
            >
          </template>
        </el-table-column>
        <el-table-column label="工号" prop="workNumber" />
        <el-table-column label="聘用形式" prop="formOfEmployment" />
        <el-table-column label="部门" prop="departmentName" />
        <el-table-column label="入职日期" prop="timeOfEntry" />
        <el-table-column label="操作" width="300" fixed="right">
          <template v-slot="{ row }">
            <div>
              <el-button type="text" @click="look(row)">查看</el-button>
              <el-button type="text">转正</el-button>
              <el-button type="text">调岗</el-button>
              <el-button type="text">离职</el-button>
              <el-button type="text" @click="role(row)">角色</el-button>
              <el-button type="text" @click="del(row)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="page">
        <el-pagination
          :current-page="page.page"
          :page-sizes="[1, 2, 5, 10, 20]"
          :page-size="page.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <add ref="add" />
    <role ref="role" v-model="isRole" />
    <el-dialog title="头像二维码" :visible.sync="isQrcode">
      <div style="text-align: center">
        <canvas ref="canvas" />
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { sysUser, sysUserDel } from '@/api/employees'
import employeesData from '@/api/constant/employees'
import Add from './components/Add.vue'
import Role from './components/Role.vue'
import dayJs from 'dayjs'
import Qrcode from 'qrcode'
export default {
  components: { Add, Role },
  data() {
    return {
      list: [],
      isRole: false,
      defaultImg: require('@/assets/common/bigUserHeader.png'),
      isQrcode: false,
      page: {
        page: 1,
        size: 10,
        total: 30
      }
    }
  },
  created() {
    this.getData()
  },
  methods: {
    handleSizeChange(size) {
      this.page.size = size
      this.page.page = 1
      this.getData()
    },
    handleCurrentChange(page) {
      this.page.page = page
      this.getData()
    },
    async getData() {
      const { data } = await sysUser(this.page)
      // 处理应聘形式数据
      data.rows.forEach((item) => {
        const temp = employeesData.hireType.find((val) => {
          return val.id === +item.formOfEmployment
        })
        item.formOfEmployment = temp ? temp.value : '--'
      })
      // 处理时间格式
      data.rows.forEach((item) => {
        item.timeOfEntry = dayJs(item.timeOfEntry).format('YYYY-MM-DD')
      })
      this.list = data.rows
      this.page.total = data.total
    },
    // 添加打开弹框
    addClick() {
      this.$refs.add.dialogVisible = true
    },
    // 删除
    del(row) {
      this.$confirm('确认删除该员工吗', '提示')
        .then(async() => {
          await sysUserDel(row.id)
          this.$message.success('删除成功')
          this.handleCurrentChange(1)
        })
        .catch(() => {})
    },
    // 查看
    look(row) {
      this.$router.push('/employees/detail/' + row.id)
    },
    // 角色
    role(row) {
      this.$refs.role.getData(row.id)
      this.isRole = true
    },
    // 展示头像二维码
    showQrcode(imgUrl) {
      if (!imgUrl) return
      this.isQrcode = true
      // dom 渲染需要时间,所以在dom渲染完成之后才能获取
      this.$nextTick(() => {
        Qrcode.toCanvas(this.$refs.canvas, imgUrl, { width: 200, height: 200 })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.employees {
  padding: 10px;
  .page {
    text-align: right;
    margin-top: 10px;
  }
  .img {
    width: 30px;
    height: 30px;
    // object-fit: contain;
  }
}
</style>
